package net.qhzw.vip.qiyu.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.mapper.GuaranteeSiteMapper;
import net.qhzw.vip.qiyu.model.BigDataInfoFaskModel;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysIndexService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("sys/index")
@Api(tags = "首页")
public class SysIndexController {

    @Autowired
    private SysIndexService sysIndexService;
    @Autowired
    private GuaranteeSiteMapper guaranteeSiteMapper;

    @ApiOperation(value = "查询租车,换车,退车count")
    @GetMapping("rentalCount")
    public JsonResult<Map<String, Integer>> rentalCount() {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Integer> result = sysIndexService.rentalCount(shopId);
        //Map<String, Integer> result = new HashMap<>();
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "查询担保站点风险预警")
    @GetMapping("riskWarning")
    public JsonResult<Map<String, Integer>> riskWarning(BigDecimal amount) {
        Map<String, Integer> result = sysIndexService.riskWarning(amount);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "查询租车量")
    @GetMapping("carRentalCount")
    public JsonResult carRentalCount() {
        BigDataInfoFaskModel foundationData = guaranteeSiteMapper.getFoundationData();
        if (foundationData != null && foundationData.getIsFake() == 1) {
            Map<String, Integer> result = new HashMap<>();
            //历史租车总量totalCar
            //历史组电池总量totalBattery
            //今日租车辆todayCar
            //今日租电池量todayBattery
            result.put("totalCar", (new Random().nextInt(3) + 1) * (foundationData.getRentTheuser() == null ? 623 : foundationData.getRentTheuser()) + 50);
            result.put("totalBattery", (new Random().nextInt(2) + 1) * (foundationData.getRentTheuser() == null ? 623 : foundationData.getRentTheuser()));
            result.put("todayCar", Integer.valueOf(foundationData.getLeaseCar()));
            result.put("todayBattery", (new Random().nextInt(11) + 10));
            return JsonResult.ok(result);
        }


        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Integer> result = sysIndexService.carRentalCount(shopId);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "查询今日退车量")
    @GetMapping("returnAmount")
    public JsonResult returnAmount() {
        BigDataInfoFaskModel foundationData = guaranteeSiteMapper.getFoundationData();
        if (foundationData != null && foundationData.getIsFake() == 1) {
            Map<String, Integer> result = new HashMap<>();
            //历史租车总量totalCar
            //历史组电池总量totalBattery
            //今日租车辆todayCar
            //今日租电池量todayBattery
            result.put("todayCar", Integer.valueOf(foundationData.getReturnCar()));
            result.put("todayBattery", (new Random().nextInt(5) + 1));

            return JsonResult.ok(result);
        }
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Integer> result = sysIndexService.returnAmount(shopId);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "查询今日收入")
    @GetMapping("queryTodaysIncome")
    public JsonResult queryTodaysIncome() {

        BigDataInfoFaskModel foundationData = guaranteeSiteMapper.getFoundationData();
        if (foundationData != null && foundationData.getIsFake() == 1) {
            // 生成大于 0 小于等于 0.6 的随机小数
            double randomNum =new Random().nextDouble()*0.6 + 0.0;

            //int i = new Random().nextInt(10) + 1;

            DecimalFormat df = new DecimalFormat("0.00");
            return JsonResult.ok(df.format(20000.48 + (20000 * randomNum)));
        }
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        BigDecimal result = sysIndexService.queryTodaysIncome(shopId);
        return JsonResult.ok(result);
    }


    @ApiOperation(value = "查询结余欠款")
    @GetMapping("balanceArrears")
    public JsonResult balanceArrears() {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        BigDecimal result = sysIndexService.balanceArrears(shopId);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "查询维修欠款")
    @GetMapping("maintArrs")
    public JsonResult maintArrs() {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        BigDecimal maintFees = sysIndexService.maintArrs(shopId);
        return JsonResult.ok(maintFees);
    }


    @ApiOperation(value = "导出今日收入明细")
    @GetMapping("exportIncomeToday")
    public void exportIncomeToday(HttpServletResponse response) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        sysIndexService.exportIncomeTodayDetails(response, shopId);
    }


    /*@ApiOperation(value = "历史骑遇骑源租车总量")
    @GetMapping("historicalCarRentals")
    public JsonResult historicalCarRentals() {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Integer> result = sysIndexService.historicalCarRentals(shopId);
        return JsonResult.ok(result);
    }*/


}
